package aip.core.dao.information;

import static org.springframework.util.Assert.hasText;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.dao.DataAccessException;
import org.springframework.util.StringUtils;

import aip.core.model.information.Information;
import aip.ui.user.util.UserHelper;

import com.googlecode.horncomb.bean.filter.ConditionSet;
import com.googlecode.horncomb.bean.filter.SimpleMapConditionSet;
import com.googlecode.horncomb.bean.order.DefaultOrder;
import com.googlecode.horncomb.bean.order.DefaultOrderToken;
import com.googlecode.horncomb.bean.order.Order;
import com.googlecode.horncomb.bean.paged.PagedList;
import com.googlecode.horncomb.orm.ibatis.SqlMapEntityDaoImpl;

/**
 * Information data access object
 * <p>
 * 负责人/principal:<br/>
 * 修改记录/revision:<br/>
 * 日期:<br/>
 * 修改人:<br/>
 * 修改说明:<br/>
 * @author
 * @version $Id$
 */
public class InformationDao extends SqlMapEntityDaoImpl<Information>
{
    private static final long serialVersionUID = -5204259018978252846L;

    /**
     * {@inheritDoc}
     */
    @Override
    public PagedList<List<? extends Information>> query(
            ConditionSet constraints, Order order, int page, int pageSize)
            throws DataAccessException
    {
        String state = (String) constraints.getConstraintsMap().get("state");
        if (order == null)
        {
            order = new DefaultOrder();
        }
        if (StringUtils.hasText(state))
        {
            if (Information.STATE_PUBLISHED.equals(state))
            {
                order.addSortToken(new DefaultOrderToken("PUB_DATE_", false));

            }
            else if (Information.STATE_DRAFT.equals(state))
            {
                order.addSortToken(new DefaultOrderToken("CREATE_", false));
                order = new DefaultOrder(
                        new DefaultOrderToken("CREATE_", false));
            }
            else if (Information.STATE_DEL.equals(state))
            {
                order.addSortToken(new DefaultOrderToken("LAST_EDITDATE_",
                        false));
            }

        }
        return super.query(constraints, order, page, pageSize);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void insert(Information entity) throws DataAccessException
    {
        Date now = new Date();
        entity.setCreator(UserHelper.getUser().getUsername());
        entity.setCreate(now);
        entity.setLastEditor(UserHelper.getUser().getUsername());
        entity.setLastEditDate(now);
        if (Information.STATE_PUBLISHED.equals(entity.getState()))
        {
            entity.setPublisher(UserHelper.getUser().getUsername());
            entity.setPublishDate(new Date());
        }
        super.insert(entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void update(Information entity) throws DataAccessException
    {
        entity.setLastEditor(UserHelper.getUser().getUsername());
        entity.setLastEditDate(new Date());
        if (Information.STATE_PUBLISHED.equals(entity.getState())
                && !StringUtils.hasText(entity.getPublisher()))
        {
            entity.setPublisher(UserHelper.getUser().getUsername());
            entity.setPublishDate(new Date());
        }
        super.update(entity);
    }

    /**
     * {@inheritDoc}
     */
    public List<? extends Information> queryByType(Integer catalog,
            int pageSize, Boolean sticky, Boolean isShowSub)
            throws DataAccessException
    {
        ConditionSet cons = new SimpleMapConditionSet()
                .addConstraint("catalog", catalog)
                .addConstraint("state", Information.STATE_PUBLISHED)
                .addConstraint("sticky", sticky)
                .addConstraint("isShowSub", isShowSub);
        Order order = new DefaultOrder(
                new DefaultOrderToken("PUB_DATE_", false));
        return super.query(cons, order, 1, pageSize).getSource();
    }

    public List<? extends Information> queryByType(Integer catalog, int pageSize)
            throws DataAccessException
    {
        ConditionSet cons = new SimpleMapConditionSet().addConstraint(
                "catalog", catalog).addConstraint("state",
                Information.STATE_PUBLISHED);
        Order order = new DefaultOrder(
                new DefaultOrderToken("PUB_DATE_", false));
        return super.query(cons, order, 1, pageSize).getSource();
    }

    public List<? extends Information> queryByType(Integer catalog, int start,
            int pageSize, Boolean isShowSub) throws DataAccessException
    {
        ConditionSet cons = new SimpleMapConditionSet()
                .addConstraint("catalog", catalog)
                .addConstraint("state", Information.STATE_PUBLISHED)
                .addConstraint("isShowSub", isShowSub);
        ;
        Order order = new DefaultOrder(
                new DefaultOrderToken("PUB_DATE_", false));
        return super.query(cons, order, start, pageSize).getSource();
    }

    public List<? extends Information> queryByType(String likeStr, int start,
            int pageSize, Boolean isShowSub) throws DataAccessException
    {
        ConditionSet cons = new SimpleMapConditionSet()
                .addConstraint("likeStr", likeStr)
                .addConstraint("state", Information.STATE_PUBLISHED)
                .addConstraint("isShowSub", isShowSub);
        ;
        Order order = new DefaultOrder(
                new DefaultOrderToken("PUB_DATE_", false));
        return super.query(cons, order, start, pageSize).getSource();
    }

    public void updateState(Integer id, String state)
            throws DataAccessException
    {
        hasText(state);
        ConditionSet cons = new SimpleMapConditionSet().addConstraint("id", id)
                .addConstraint("state", state);
        if (Information.STATE_PUBLISHED.equals(state))
        {
            cons.getConstraintsMap().put("publisher",
                    UserHelper.getUser().getUsername());
            cons.getConstraintsMap().put("publishDate", new Date());
        }
        else if (Information.STATE_DEL.equals(state))
        {
            cons.getConstraintsMap().put("lastEditor",
                    UserHelper.getUser().getUsername());
            cons.getConstraintsMap().put("lastEditDate", new Date());
        }
        this.doUpdate(cons, null, "updateState");
    }

    public void updateState(Set<Integer> ids, String state)
            throws DataAccessException
    {
        hasText(state);
        ConditionSet cons = new SimpleMapConditionSet().addConstraint("ids",
                ids.toArray(new Integer[] {})).addConstraint("state", state);
        if (Information.STATE_PUBLISHED.equals(state))
        {
            cons.getConstraintsMap().put("publisher",
                    UserHelper.getUser().getUsername());
            cons.getConstraintsMap().put("publishDate", new Date());
        }
        else if (Information.STATE_DEL.equals(state)
                || Information.STATE_DRAFT.equals(state))
        {
            cons.getConstraintsMap().put("lastEditor",
                    UserHelper.getUser().getUsername());
            cons.getConstraintsMap().put("lastEditDate", new Date());
        }
        this.doUpdate(cons, null, "updateState");
    }

    public int getDataCount(Integer catalog, Boolean isShowSub)
    {
        ConditionSet constraints = new SimpleMapConditionSet()
                .addConstraint("catalog", catalog)
                .addConstraint("state", Information.STATE_PUBLISHED)
                .addConstraint("isShowSub", isShowSub);
        return super.queryCount(constraints);
    }

    public int getDataCount(String likeStr, Boolean isShowSub)
    {
        ConditionSet constraints = new SimpleMapConditionSet()
                .addConstraint("likeStr", likeStr)
                .addConstraint("state", Information.STATE_PUBLISHED)
                .addConstraint("isShowSub", isShowSub);
        return super.queryCount(constraints);
    }
}
